joy0_handler:
    .byte $ff //joy stored value
    .byte <joy0_push_up,>joy0_push_up
    .byte <joy0_release_up,>joy0_release_up
    .byte <joy0_push_down,>joy0_push_down
    .byte <joy0_release_down,>joy0_release_down 
    .byte <joy0_push_left,>joy0_push_left
    .byte <joy0_release_left,>joy0_release_left
    .byte <joy0_push_right,>joy0_push_right
    .byte <joy0_release_right,>joy0_release_right
    .byte <joy0_push_fire,>joy0_push_fire 
    .byte <joy0_release_fire,>joy0_release_fire
	
joy1_handler:
    .byte $ff //joy stored value
    .byte <joy1_push_up,>joy1_push_up
    .byte <joy1_release_up,>joy1_release_up
    .byte <joy1_push_down,>joy1_push_down
    .byte <joy1_release_down,>joy1_release_down 
    .byte <joy1_push_left,>joy1_push_left
    .byte <joy1_release_left,>joy1_release_left
    .byte <joy1_push_right,>joy1_push_right
    .byte <joy1_release_right,>joy1_release_right
    .byte <joy1_push_fire,>joy1_push_fire 
    .byte <joy1_release_fire,>joy1_release_fire
	
jump: .byte $4c,$00,$00


.var INDEX = $12
.var ACT_JOY = $14
.var PRE_JOY = $16
.var MASK_JOY = $18
.var XOR_JOY = $1a

joyhandling:
//handling joy imputs
    ldy #$00
    ldx #$00
    :setThis(joy0_handler)
	
main_joy_cycle:

    lda (THIS),Y //prev joy value
    sta PRE_JOY
    lda JOY0,X
    sta ACT_JOY
    eor PRE_JOY
    sta XOR_JOY

    lda #%00000001
    sta MASK_JOY
    lda #00
    sta INDEX    
    iny
joy_test_cycle:

    lda MASK_JOY
    and XOR_JOY
    cmp #$00
    beq skip_handling_joy 
    lda MASK_JOY
    and PRE_JOY
    cmp #$00
    beq release_joy

    //push joy0
    :copyFromThis(jump+1,2)
    jsr jump
    iny
    iny
    jmp skip_handling_joy_end

release_joy:

    iny
    iny
    :copyFromThis(jump+1,2)
    jsr jump

    jmp skip_handling_joy_end

skip_handling_joy:
    iny
    iny
    iny
    iny

skip_handling_joy_end:

    lda MASK_JOY
    asl
    sta MASK_JOY
    
	inc INDEX
	lda INDEX
    cmp #$05
    beq joy_test_cycle_end
    jmp joy_test_cycle
	
joy_test_cycle_end:

    ldy #$00
    lda ACT_JOY
    sta (THIS),Y //store joy value

    cpx #$01
    beq main_joy_cycle_end

    //setup the next cycle
    :setThis(joy1_handler)
    inx 
    jmp main_joy_cycle

main_joy_cycle_end:
	
	rts
	
joy0_push_up:
    inc $0428
	
	tya
    pha
	
	ldy #DYN_PUSHING
	lda (FIGHTER0),Y
	ora #%00000100
	sta (FIGHTER0),Y
	
	pla
    tay
	
    rts
joy0_release_up:
    dec $0428
    rts
joy0_push_down:
    inc $0429
    rts
joy0_release_down:
    dec $0429
    rts
joy0_push_right:
    inc $042a

	tya
    pha
	
	ldy #DYN_ORIENTATION
	lda #$01
	sta (FIGHTER0),Y
	
	ldy #DYN_PUSHING
	lda (FIGHTER0),Y
	ora #%00000010
	sta (FIGHTER0),Y
	
	pla
    tay
    rts
joy0_release_right:
    dec $042a
	tya
    pha
	
	ldy #DYN_PUSHING
	lda (FIGHTER0),Y
	and #%11111101
	sta (FIGHTER0),Y
	
	pla
    tay
    rts
joy0_push_left:
    inc $042b

	tya
    pha
	
	ldy #DYN_ORIENTATION
	lda #$00
	sta (FIGHTER0),Y
	
	ldy #DYN_PUSHING
	lda (FIGHTER0),Y
	ora #%00000001
	sta (FIGHTER0),Y
	
	pla
    tay
    rts
joy0_release_left:
    dec $042b
	tya
    pha

	ldy #DYN_PUSHING
	lda (FIGHTER0),Y
	and #%11111110
	sta (FIGHTER0),Y
	
	pla
    tay
    rts
joy0_push_fire:
    inc $042c
    rts
joy0_release_fire:
    dec $042c
    rts

joy1_push_up:
    inc $0450
	
	tya
    pha
	
	ldy #DYN_PUSHING
	lda (FIGHTER1),Y
	ora #%00000100
	sta (FIGHTER1),Y
	
	pla
    tay
	
    rts
joy1_release_up:
    dec $0450
    rts
joy1_push_down:
    inc $0451
    rts
joy1_release_down:
    dec $0451
    rts
joy1_push_right:
    inc $0452
	
	tya
    pha
	
	ldy #DYN_ORIENTATION
	lda #$01
	sta (FIGHTER1),Y
	
	ldy #DYN_PUSHING
	lda (FIGHTER1),Y
	ora #%00000010
	sta (FIGHTER1),Y
	
	pla
    tay
	
    rts
joy1_release_right:
    dec $0452
	
	tya
    pha
	
	ldy #DYN_PUSHING
	lda (FIGHTER1),Y
	and #%11111101
	sta (FIGHTER1),Y
	
	pla
    tay
	
    rts
joy1_push_left:
    inc $0453
	
	tya
    pha
	
	ldy #DYN_ORIENTATION
	lda #$00
	sta (FIGHTER1),Y
	
	ldy #DYN_PUSHING
	lda (FIGHTER1),Y
	ora #%00000001
	sta (FIGHTER1),Y
	
	pla
    tay
	
    rts
joy1_release_left:
    dec $0453
	
	tya
    pha

	ldy #DYN_PUSHING
	lda (FIGHTER1),Y
	and #%11111110
	sta (FIGHTER1),Y
	
	pla
    tay
	
    rts
joy1_push_fire:
    inc $0454
    rts
joy1_release_fire:
    dec $0454
    rts